 # -*- coding=utf-8 -*-

import cv2
import sys
import gc
from train import Model

def pred(casedir='/usr/lib64/python3.6/site-packages/cv2/data/haarcascade_frontalface_alt2.xml'):
    model = Model()
    model.load_md('./model/model.h5')
    color = (0,255,0)
    cap = cv2.VideoCapture(int(0))
    while True:
        _,frame = cap.read()

        frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

        cascade = cv2.CascadeClassifier(casedir)

        faceRects = cascade.detectMultiScale(frame_gray, scaleFactor=1.2, minNeighbors=3, minSize=(32, 32))

        if len(faceRects) > 0:
            for faceRect in faceRects:
                x, y, w, h = faceRect
                image = frame[y - 10: y + h + 10, x - 10: x + w + 10]
                faceID = model.face_predict(image)
                if faceID == 0:
                    cv2.rectangle(frame, (x - 10, y - 10), (x + w + 10, y + h + 10), color, thickness=2)
                    print(u"这是训练数据集里的脸")
                    cv2.putText(frame,'Is my face',
                                (x+30, y+30),
                                cv2.FONT_HERSHEY_COMPLEX,
                                1,
                                (255,0,255),
                                2)
                else:
                    cv2.rectangle(frame, (x - 10, y - 10), (x + w + 10, y + h + 10), color, thickness=2)
                    print(u"这不是训练数据集里的脸")
                    cv2.putText(frame, 'Is not my face',
                                (x + 30, y + 30),
                                cv2.FONT_HERSHEY_COMPLEX,
                                1,
                                (255, 0, 255),
                                2)

        cv2.imshow(u"PredictFace",frame)
        k = cv2.waitKey(10)
        if k & 0xFF == ord('q'):
            break

    cap.release()
    cv2.destroyAllWindows()